package com.alibaba.csp.sentinel.dashboard.rule.convert;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.fastjson.JSON;

/**
 * @author T6001270
 * @since 2020/9/30 4:18
 */
public class DegradeRuleConvert implements Converter<List<DegradeRuleEntity>, String> {

    @Override
    public String convert(List<DegradeRuleEntity> degradeRuleEntities) {
        if(degradeRuleEntities==null){
            return null;
        }
        List<DegradeRule> rules = new ArrayList<>();
        for (DegradeRuleEntity entity : degradeRuleEntities) {
            DegradeRule rule = new DegradeRule();
            rule.setResource(entity.getResource());
            rule.setLimitApp(entity.getLimitApp());
            if(entity.getCount()!=null){
                rule.setCount(entity.getCount());
            }
            if(entity.getTimeWindow()!=null){
                rule.setTimeWindow(entity.getTimeWindow());
            }
            if(entity.getGrade()!=null){
                rule.setGrade(entity.getGrade());
            }
            if(entity.getMinRequestAmount()!=null){
                rule.setMinRequestAmount(entity.getMinRequestAmount());
            }
            if(entity.getSlowRatioThreshold()!=null){
                rule.setSlowRatioThreshold(entity.getSlowRatioThreshold());
            }
            if(entity.getMinRequestAmount()!=null){
                rule.setMinRequestAmount(entity.getMinRequestAmount());
            }
            if(entity.getStatIntervalMs()!=null){
                rule.setStatIntervalMs(entity.getStatIntervalMs());
            }
            
            rules.add(rule);
        }
        return JSON.toJSONString(rules,true);
    }
}
